package site.cyningxu.analyzer;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import site.cyningxu.client.DyBulletScreenClient;
import site.cyningxu.utils.FileUtil;
import site.cyningxu.utils.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by xuxingcan on 17-8-15.
 */
public class AnalyzeMsg extends Thread {
    private static Gson gson = new Gson();
    public static final List<String> MSG_LIST = Collections.synchronizedList(new ArrayList());
    private final List<String> temp_list = new ArrayList<>();
    private List<AnalyzerModel> analyzerModelList;

    @Override
    public void run() {
        try {
            analyzerModelList = gson.fromJson(FileUtil.readFile(""),
                    new TypeToken<List<AnalyzerModel>>() {
                    }.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (analyzerModelList == null || analyzerModelList.size() == 0) {
            return;
        }
        //获取弹幕客户端
        DyBulletScreenClient danmuClient = DyBulletScreenClient.getInstance();

        //判断客户端就绪状态
        while (danmuClient.getReadyFlag()) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            temp_list.clear();
            temp_list.addAll(MSG_LIST);
            MSG_LIST.clear();
            for (AnalyzerModel analyzerModel : analyzerModelList) {
                analyzerModel.num = 0;
            }
            for (String s : temp_list) {
                Log.i(s);
                for (AnalyzerModel analyzerModel : analyzerModelList) {
                    if (analyzerModel.msgList.contains(s)) {
                        analyzerModel.num++;
                        break;
                    }
                }
            }
            Log.i("-----------end total " + temp_list.size() + "-----------");
            analyzerModelList.sort(new Comparator<AnalyzerModel>() {
                @Override
                public int compare(AnalyzerModel o1, AnalyzerModel o2) {
                    return o1.num - o2.num;
                }
            });
            if (analyzerModelList.get(0).num == 0) {
                Collections.shuffle(analyzerModelList);
            }
            Log.i("final key " + analyzerModelList.get(0).key);
        }
    }
}
